<template>
    <div v-if="showFlag" v-loading="isLoading">
        <div v-if="this.addressList.length === 0" style="margin-left: 100px;font-size: 16px">
            <span >您还没有收货地址哦~</span>
        </div>
        <div v-else class="address-box" v-for="(item, index) in addressList" :key="index">
            <div class="address-header">
                <span>{{item.userName}}</span>
                <div class="address-action">
                    <span @click="edit(index)"><Icon type="edit"></Icon> 修改</span>
                    <span @click="del(index)"><Icon type="trash-a"></Icon> 删除</span>
                </div>
            </div>
            <div class="address-content">
                <p><span class="address-content-title"> 收 货 人 :</span> {{item.userName}}</p>
                <p><span class="address-content-title">电话号码:</span> {{item.tel}}</p>
                <p><span class="address-content-title">收货地址:</span> {{item.streetName}}</p>
            </div>
        </div>
        <Modal v-model="modal" width="530">
            <p slot="header">
                <Icon type="edit"></Icon>
                <span>修改地址</span>
            </p>
            <div>
                <Form :model="formData" ref="formData" label-position="left" :label-width="100" :rules="ruleInline">
                    <FormItem label="收件人" prop="name">
                        <i-input v-model="formData.name" size="large"></i-input>
                    </FormItem>
                    <FormItem label="收件地址" prop="address">
                        <i-input v-model="formData.address" size="large"></i-input>
                    </FormItem>
                    <FormItem label="手机号码" prop="phone">
                        <i-input v-model="formData.phone" size="large"></i-input>
                    </FormItem>
                </Form>
            </div>
            <div slot="footer">
                <Button type="primary" size="large" long @click="editAction('formData')">修改</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    import {mapActions, mapGetters} from 'vuex';
    import Distpicker from 'v-distpicker';

    export default {
        name: 'MyAddress',
        data() {
            return {
                modal: false,
                addressid: '',
                addressList: [],
                formData: {
                    name: '',
                    address: '',
                    phone: '',
                    postalcode: '',
                },
                ruleInline: {
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ],
                    address: [
                        {required: true, message: '请输入地址', trigger: 'blur'}
                    ],
                    phone: [
                        {required: true, message: '手机号不能为空', trigger: 'blur'},
                        {type: 'string', pattern: /^1[3|4|5|7|8][0-9]{9}$/, message: '手机号格式出错', trigger: 'blur'}
                    ]
                },
                showFlag:false,//加载页面
                isLoading:true//
            };
        },
        methods: {
            ...mapActions(["setUserAddress",]),
            edit(index) {
                this.modal = true;
                this.addressid = this.addressList[index].addressId;
                this.formData.address = this.addressList[index].streetName;
                this.formData.name = this.addressList[index].userName;
                this.formData.phone = this.addressList[index].tel;
            },
            editAction(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios
                            .post("/item/updateaddress", {
                                "addressId": this.addressid,
                                "isDefault": false,
                                "streetName": this.formData.address,
                                "tel": this.formData.phone,
                                "userId": this.$store.getters.getUser.id,
                                "userName": this.formData.name
                            })
                            .then(res => {
                                if (res.data.result != -1) {
                                    this.modal = false;
                                    this.getUserAddressList();
                                    this.address = this.getUserAddress;
                                } else {
                                    this.notifyError("修改失败");
                                }
                            })
                            .catch(err => {
                                return Promise.reject(err);
                            });
                    } else {
                        this.notifyError("请填写完整信息")
                    }
                })

            },
            del(index) {
                this.$confirm('此操作将永久删除该地址, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios
                        .delete("/item/deladdress", {
                            params: {
                                "addressId": this.addressList[index].addressId
                            }
                        })
                        .then(res => {
                            if (res.data.result != -1) {
                                this.notifySucceed("删除成功");
                                setTimeout(() => {
                                    this.getUserAddressList();
                                }, 500)
                            } else {
                                this.notifyError("删除失败");
                            }
                        })
                        .catch(err => {
                            return Promise.reject(err);
                        });
                }).catch(() => {

                });

            },
            getUserAddressList() {
                this.$axios
                    .get("/item/getlistaddress", {
                        params: {
                            userId: this.$store.getters.getUser.id
                        }
                    })
                    .then(res => {
                        this.setUserAddress(res.data.result);
                        this.addressList = res.data.result;
                        this.showFlag = true;
                        this.isLoading = false;
                    })
                    .catch(err => {
                        return Promise.reject(err);
                    });
            }
        },
        created() {
            this.getUserAddressList();
        },
        components: {
            Distpicker
        },
        computed: {
            ...mapGetters(["getUserAddress"]),
        },
    };
</script>

<style scoped>
    .address-box {
        padding: 15px;
        margin: 15px;
        border-radius: 5px;
        box-shadow: 0px 0px 5px #ccc;
    }

    .address-header {
        height: 35px;
        display: flex;
        justify-content: space-between;
        color: #232323;
        font-size: 18px;
    }

    .address-content {
        font-size: 14px;
    }

    .address-content-title {
        color: #999;
    }

    .address-action span {
        margin-left: 15px;
        font-size: 14px;
        color: #2d8cf0;
        cursor: pointer;
    }
</style>
